#! /usr/bin/python2.4

# Copyright 2008 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""bypass.py unittest"""


import unittest

import svn.fs
import svn.repos

import gvn.hooks.info
import testing.hooks.common

# import RunHook to pass the function pointer to testcommon in unittests
from gvn.hooks.pre_commit.bypass import RunHook


class TestCase(testing.hooks.common.HookTestCase):

  def setUp(self):
    # HookTestCase creates and sets a _logger object based on whether
    # GVN_TEST_DEBUG is exported or not
    testing.hooks.common.HookTestCase.setUp(self)

    # create group pickle to use as our group file
    user_groups= {
      "polly": [ "group1" ],
      "adminuser": [ testing.hooks.common.SUPER_GROUP ],
    }

    for user in user_groups:
      self.userdb.WriteUserGroups(user, user_groups[user])


  def testSuperUserGvnSuperUsersProp(self):
    """Check setting superusers property from superuser.
    """
    self.assertHookResult(self._TestProp(RunHook, "gvn:superusers",
                          "group: svn-team", author="adminuser"), 0)

  def testRandomUserGvnSuperUsersProp(self):
    """Check setting superusers property from a regular user.
    """
    self.assertHookResult(self._TestProp(RunHook, "gvn:superusers",
                          "group: fowls", author="polly"), 0)

  def testSuperUserGvnBypassHookProp(self):
    """Check setting gvn:bypass-hooks property from a superuser.
    """
    self.assertHookResult(self._TestProp(RunHook, "gvn:bypass-hooks", "1",
                          author="adminuser", path="REV"), -1)

  def testRandomUserGvnBypassHookProp(self):
    """Check setting gvn:bypass-hooks property from a regular user.
    """
    self.assertHookResult(self._TestProp(RunHook, "gvn:bypass-hooks", "1",
                         author="polly", path="REV"),
                         "Non Superuser polly tried to invoke gvn:bypass-hooks")


if __name__ == "__main__":
  unittest.main()
